<?php
/**
 * DatabaseController.php
 *
 * @category    Madoqua
 * @package     Module
 * @subpackage  Install
 */

/** base class */
require_once 'Madoqua/Controller/Action/Abstract.php';

/**
 * Install_DatabaseController
 *
 * @category    Madoqua
 * @package     Module
 * @subpackage  Install
 */
class Install_DatabaseController extends Madoqua_Controller_Action_Abstract
{
    /**
     * initialization
     */
    public function init()
    {
        parent::init();

        $this->_modelLoader->load('BlogAdmin_Config');
        //load the config model
    }

    /**
     * start of db installaton
     */
    public function indexAction()
    {
        $form = $this->_formLoader->get('Install_Database_Adapter');
        //db adapter select form

        $form->setDefaults(array('adapter' => $this->_config->database->adapter));
        //set the default value for adapter

        $this->view->form = $form;
        //select adapter form

        if ($form->isPrg() && $form->isValidPrg()) {
            //woo! valid!

            $adapter = $form->getValue('adapter');
            //the requested database adapter

            $this->_config->database->adapter = $adapter;
            //set the relevant adapter
            
            BlogAdmin_Config::saveApplicationConfig();
            //save the config

            $options = array(
                'module' => 'install',
                'controller' => 'database',
                'action' => $adapter
            );
            Madoqua_Redirector::redirectAndExit($options, 'default', true);
            //redirect to the action that matches the adapter
        }
    }

    /**
     * Pdo_Sqlite Adapter
     */
    public function sqliteAction()
    {
    }

    /**
     * Pdo_Mysql Adapter
     */
    public function pdomysqlAction()
    {
        $this->mysql('pdo_mysql');
        //look down... down there!
    }

    /**
     * Mysqli Adapter
     */
    public function mysqliAction()
    {
        $this->mysql('mysqli');
        //look down... down there! (again)
    }

    /**
     * Pdo_Sqlite action
     */
    public function pdosqliteAction()
    {
        //sqlite doesn't really need any setting up
        
        $this->getHelper('viewRenderer')->setNoRender();
        //so no view
        
        $databaseFile = Madoqua_Application::getDataDirectory() . DIRECTORY_SEPARATOR .  'database/db.sl3';
        $this->_config->database->dbname = $databaseFile;
        //it's just a file, after all
        
        // Set this explicitly as there is no choice of sqlite db to use
        $this->_config->database->prefix = 'madoqua_';        
        
        BlogAdmin_Config::saveApplicationConfig();
        //save the config
        
        $this->redirectToUserInstall();
        //go away from this awful controller, now!
    }

    /**
     * set up mysql after database details have been retrieved
     */
    public function setupMysqlAction()
    {
        $this->_modelLoader->load('Install_Mysql');
        $mysql = new Install_Mysql();
        //mysql setup model

        if (!$mysql->canInstall()) {
            //meh.. requirements fail :(

            $this->view->success = false;

        } elseif ($mysql->install()) {
            //well... Install seems to have worked...

            if ($mysql->isInstalled()) {
                //but let's make sure!

                $this->redirectToUserInstall();
                //carry on waywards, son
            }

            $this->view->success = false;
            //we failed :^[
        }
    }

    /**
     * generalized mysql handling (except the adapter to use nothing changes)
     *
     * @param string $adapter
     * @return void
     */
    private function mysql($adapter) {
        $form = $this->_formLoader->get('Install_Database_Mysql');

        $form->setDefaults($this->_config->database->toArray());
        //set defaults

        $this->view->form = $form;
        //form
        if ($form->isPrg() && $form->isValidPrg()) {
            //valid form post

            $values = $form->getValues();;
            unset($values['s']);
            //remove submit
            $this->_config->database = $values;
            $this->_config->database->adapter = $adapter;
            //blugh, this is ugly XD

            BlogAdmin_Config::saveApplicationConfig();
            //save the config

            $options = array(
                'module' => 'install',
                'controller' => 'database',
                'action' => 'setup-mysql'
            );
            Madoqua_Redirector::redirectAndExit($options, 'default');
            //go to user setup
        }

        $this->render('mysql');
        //always render mysql.phtml (no need for two view scripts)
    }
    
    /**
     * redirect to user install (well durrr)
     * 
     * @return void
     */
    private function redirectToUserInstall()
    {
        $options = array(
            'module' => 'install',
            'controller' => 'user'
        );
        Madoqua_Redirector::redirectAndExit($options, 'default', true);
        //go to user setup        
    }

}